# -*- coding: utf-8 -*-
__author__ = 'bobby'

import django_filters
from django.db.models import Q

from .models import InvolverInfo, InvolverMotionPath


class InvolverMotionPathFilter(django_filters.rest_framework.FilterSet):
    time_range = django_filters.DateTimeFromToRangeFilter(method='time_range__filter')

    def time_range__filter(self, queryset, name, value):
        q1 = Q()
        q1.connector = 'OR'
        q2 = Q()
        q2.connector = 'OR'
        if value.start and value.stop:
            q1 = Q(begin_time__range=[value.start, value.stop])
            q2 = Q(end_time__range=[value.start, value.stop])
            return queryset.filter(q1 | q2)
        return queryset

    class Meta:
        model = InvolverMotionPath
        fields = "__all__"
